package pl.diagnoser.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import pl.diagnoser.client.tools.DataGetterService;
import pl.diagnoser.client.tools.DataGetterServiceAsync;
import pl.diagnoser.client.tools.DiagnoserProperties;
import pl.diagnoser.client.tools.LogIn;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class AddNewObjectLayout extends VLayout {
	private DiagnoserProperties properties = new DiagnoserProperties();
	private static DataGetterServiceAsync dataGetterAsc = GWT.create(DataGetterService.class);
	private static Long ident;
	private static Window newObjectWindow;
	private static ObjectHandling layout;
	private static SelectItem combo;
	
	AddNewObjectLayout(Window window, ObjectHandling parrentLayout, SelectItem objectCombo) {
		newObjectWindow = window;
		layout = parrentLayout;
		combo = objectCombo;
	}
	
	public VLayout getNewObject() {
		
		this.setPadding(10);
		this.setMembersMargin(5);
		
		final ObjectDetailsForm form = new ObjectDetailsForm(false, false, true, false);
		
        HLayout buttonLayout = new HLayout();
        buttonLayout.setAlign(Alignment.CENTER);
        buttonLayout.setMembersMargin(10);
        
        IButton saveNewObjectBt = new IButton("saveNewObjectBt");
        properties.getWidgetTitle("saveNewObjectBt", saveNewObjectBt);
        
        saveNewObjectBt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(form.validate()) {
					List<FormItem> formItems = Arrays.asList(form.getFields());
					HashMap<String, String> attributeValue = new HashMap<String, String>();
					HashMap<String, List<String>> multipleAttributeValue = new HashMap<String, List<String>>();
					String objectName = null;
					String objectDate = null;
					int i = 0;
					for(FormItem formItem : formItems) {
						if(!formItem.getName().equals("combo")) {
					if(formItem instanceof SelectItem) {
								SelectItem tmpSelectItem = (SelectItem) formItem;
								if(!tmpSelectItem.getDisplayValue().isEmpty())
									attributeValue.put(formItem.getName(), tmpSelectItem.getDisplayValue());
							}
							else if(formItem instanceof TextItem){
								TextItem tmpTextItem = (TextItem) formItem;
								if(!tmpTextItem.getDisplayValue().isEmpty()) {
									i++;
									if(i == 1)
										objectName = tmpTextItem.getDisplayValue();
									else if(LogIn.getInstance().getUser().getAppId() != LogIn.SEARCH_APP && i ==2)
										objectName += " " + tmpTextItem.getDisplayValue();
									attributeValue.put(formItem.getName(), tmpTextItem.getDisplayValue());
								}
							}
							else if(formItem instanceof DateItem){
								DateItem tmpDateItem = (DateItem) formItem;
								if(!tmpDateItem.getDisplayValue().isEmpty()) {
									objectDate = tmpDateItem.getDisplayValue();
									attributeValue.put(formItem.getName(), tmpDateItem.getDisplayValue());
								}
							}
							else if(formItem instanceof ButtonItem) {}
							else if(formItem instanceof CanvasItem) {
								ListGrid tmpListGridItem = ((ListGrid)((CanvasItem) formItem).getCanvas());
								List<String> tmpMultipleAttributesValues = new ArrayList<String>();
								if(tmpListGridItem.isVisible()) {
									for(ListGridRecord record : Arrays.asList(tmpListGridItem.getRecords())) {
										tmpMultipleAttributesValues.add(record.getAttributeAsString(formItem.getName().replace("_ci", "").toLowerCase() + "_lg"));
									}
									multipleAttributeValue.put(formItem.getName().replace("_ci", "").toLowerCase(), tmpMultipleAttributesValues);
								}
							}
						}
					}
					if (dataGetterAsc == null) {
						dataGetterAsc = GWT.create(DataGetterService.class);
					}
					
					AsyncCallback<Long> callback = new AsyncCallback<Long>() {
						public void onFailure(Throwable caught) {
							SC.say("Zapis zakończony niepowodzeniem");
							System.out.println(caught.getMessage());
						}
	
						public void onSuccess(Long result) {
							ident = result;
							layout.refreshObjectList(combo, ident);
							newObjectWindow.destroy();
						}
					};
					
					dataGetterAsc.saveNewObject(objectName, objectDate, attributeValue,multipleAttributeValue,callback);
				}
			}
		});
        
        IButton eraseNewObjectFormBt = new IButton("eraseNewObjectFormBt");
        properties.getWidgetTitle("eraseNewObjectFormBt", eraseNewObjectFormBt);
        
        eraseNewObjectFormBt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.clearForm();
			}
		});
        buttonLayout.addMember(saveNewObjectBt);
        buttonLayout.addMember(eraseNewObjectFormBt);
        
        this.addMember(form);
        this.addMember(buttonLayout);
        
		return this;
	}
	
	public Long getIdent() {
		return ident;
	}
}